﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpCompare
{
    public partial class FormSession : Form
    {
        public delegate void TransfDelegate(SessionObj value);
        private SessionObj theObj { get; set; }
        public FormSession(SessionObj currentSession,string txt)
        {
            InitializeComponent();
            FormMain.currentSession = currentSession;
            this.Text = txt;
        }

        private void FormSession_Load(object sender, EventArgs e)
        {
            if (FormMain.currentSession != null)
                textBoxName.Text = FormMain.currentSession.name;
            BindTreeView(FormMain.Sessions, treeViewM);
        }

        private void ButtonOk_Click(object sender, EventArgs e)
        {
            if (FormMain.Sessions == null)
            {
                FormMain.Sessions = new List<SessionObj>();
            }
            var lists = FormMain.Sessions.Where(o => o.name != FormMain.currentSession.name).ToList();
            if (FormMain.currentSession.name == "New")
            {
                lists = FormMain.Sessions.ToList();
            }
            FormMain.currentSession.name = textBoxName.Text;                       
            lists.Add(FormMain.currentSession);
            FormMain.Sessions = lists;
            string jsons = JsonConvert.SerializeObject(lists);
            FileHelper.TxtWrite(FormMain.cfgpath, jsons);

            BindTreeView(FormMain.Sessions, treeViewM);
            TransfEvent(FormMain.currentSession);
        }

        private void BindTreeView(List<SessionObj> Rows, TreeView tree)
        {
            try
            {
                tree.Nodes.Clear();
                SessionObj root = new SessionObj();
                TreeNode rootNode = new TreeNode();
                rootNode.Tag = root;
                rootNode.Text = "根目录";
                tree.Nodes.Add(rootNode);
                if (Rows != null)
                {
                    foreach (var item in Rows)
                    {
                        TreeNode node = new TreeNode();
                        node.Tag = item;
                        node.Text = item.name;
                        rootNode.Nodes.Add(node);
                    }

                    tree.Nodes[0].ExpandAll();
                    tree.SelectedNode = tree.Nodes[0];
                }
            }
            catch { }
        }
        public event TransfDelegate TransfEvent;
        private void TreeViewM_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            FormMain.currentSession = treeViewM.SelectedNode.Tag as SessionObj;

            TransfEvent(FormMain.currentSession);

            this.Close();
        }

        private void ButtonDel_Click(object sender, EventArgs e)
        {
            if (theObj != null)
            {
                var lists = FormMain.Sessions.Where(o => o.name != theObj.name).ToList();
                FormMain.Sessions = lists;
                string jsons = JsonConvert.SerializeObject(lists);
                FileHelper.TxtWrite(FormMain.cfgpath, jsons);

                BindTreeView(FormMain.Sessions, treeViewM);
            }
        }

        private void TreeViewM_AfterSelect(object sender, TreeViewEventArgs e)
        {
            theObj = treeViewM.SelectedNode.Tag as SessionObj;
        }
    }
}
